package com.weixing.mall.provider.controller.front;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.weixing.mall.base.constant.GlobalConstant;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.auth.SysAuth;
import com.weixing.mall.core.support.auth.SysInfo;
import com.weixing.mall.provider.api.model.vo.ProductStockDetailsVo;
import com.weixing.mall.provider.model.domain.Depot;
import com.weixing.mall.provider.model.domain.ProductStock;
import com.weixing.mall.provider.model.domain.ProductStockDetails;
import com.weixing.mall.provider.model.dto.APPDepotDto;
import com.weixing.mall.provider.model.dto.InDepotBillDto;
import com.weixing.mall.provider.model.dto.MatchDepotDto;
import com.weixing.mall.provider.service.IDepotService;
import com.weixing.mall.provider.service.IInDepotBillService;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import com.weixing.mall.provider.service.IProductStockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName FrontInDepotBillController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/11/12
 * @Version 1.0
 **/
@RequestMapping("front/inDepotBill")
@RestController
@Api(value = "前端：App仓入库api", tags = "入库前端：App仓入库")
public class FrontInDepotBillController {

    @Autowired
    private IInDepotBillService inDepotBillService;
    @Autowired
    private IProductStockService productStockService;
    @Autowired
    private IProductStockDetailsService productStockDetailsService;
    @Autowired
    private IDepotService depotService;

    @ApiOperation(value = "保存入库单及关联商品信息")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@SysInfo SysAuth auth, @RequestBody InDepotBillDto inDepotBillDto) {
        inDepotBillService.save(inDepotBillDto, auth.getUid());
        return ResultUtil.success();
    }

    @ApiOperation(value = "APP仓库退换货入APP仓库")
    @PostMapping("appSave")
    @NoRepeatSubmit
    public Result appSave(@SysInfo SysAuth auth, @RequestBody APPDepotDto aPPDepotDto) {
        try {

            ProductStockDetails details = productStockDetailsService.getOne(new QueryWrapper<ProductStockDetails>().eq("stock_sn", aPPDepotDto.getDepotSn()));
            if (ObjectUtil.isNotNull(details)) {
                return ResultUtil.error("库存编码存在重复，请检查！");
            }

        } catch (Exception e) {
            return ResultUtil.error("库存编码查询存在异常，请检查后在重新提交");
        }
        inDepotBillService.appSave(aPPDepotDto, auth);
        return ResultUtil.success();
    }

    @ApiOperation(value = "APP查询库位信息")
    @GetMapping("/appStoreHouse")
    public Result getStoreHouse(Long productId) {
        Depot depot = depotService.getDepotByCode(GlobalConstant.DepotCode.APP);
        if (null == depot) {
            throw new BusinessException("仓库编号存在错误！");
        }
        ProductStock stock = productStockService.getOne(new QueryWrapper<ProductStock>().eq("product_id", productId).eq("depot_id", depot.getId()));
        return ResultUtil.success().setData(ObjectUtil.isNotEmpty(stock) ? stock.getStorehouse() : null);
    }

    /**
     * 用于前端接受扫描匹配仓库库存。
     *
     * @param matchDepotDto
     * @return
     */
    @ApiOperation(value = "匹配库存")
    @PostMapping("/matchDepot")
    public Result matchDepot(@RequestBody MatchDepotDto matchDepotDto) {
        ProductStockDetailsVo stockDetails = productStockDetailsService.matching(matchDepotDto.getGoodsId(), matchDepotDto.getColor(), matchDepotDto.getSize());
        return ResultUtil.success(ObjectUtil.isNotEmpty(stockDetails) ? stockDetails : null);
    }

    /**
     * 用于前端生成库存编码，没有库位的库存编码  订单服务失败，返回1，CMS服务失败，返回2，库位不符合要求返回3  用于前端提示那个服务失败，
     *
     * @param orderId      订单ID
     * @param serialNumber 序号
     * @return
     */
    @ApiOperation(value = "匹配库存")
    @GetMapping("/creatStockSn/{orderId}/{serialNumber}")
    public Result creatStockSn(@PathVariable Long orderId, @PathVariable String serialNumber) {
        String tt = inDepotBillService.depotStockSn(orderId, serialNumber, null);
        if (tt.equals("1")) {
            return ResultUtil.error("订单服务失败");
        }
        if (tt.equals("2")) {
            return ResultUtil.error("CMS服务失败");
        }
        return ResultUtil.success().setData(tt);
    }

    @ApiOperation(value = "测试库存编码")
    @NoNeedAccess
    @GetMapping("/test/{orderId}/{serialNumber}/{stockSn}")
    public Result test(@PathVariable Long orderId, @PathVariable String serialNumber, @PathVariable String stockSn) {
        String tt = inDepotBillService.depotStockSn(orderId, serialNumber, stockSn);
        return ResultUtil.success(tt);
    }
}
